package com.musemun.service.function.functionimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.musemun.common.ResultCode;
import com.musemun.exception.ApiException;
import com.musemun.pojo.Abnormal;
import com.musemun.pojo.Crifo;
import com.musemun.pojo.Vo.CrifoListVo;
import com.musemun.pojo.Vo.HouseListVo;
import com.musemun.service.AbnormalService;
import com.musemun.service.CrifoService;
import com.musemun.service.function.CulturalRelicsNumber;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author winner
 * @description: 文物清点类
 * @date 2020/4/19
 */
@Service
public class CulturalRelicsNumberImpl implements CulturalRelicsNumber {
    //文物表操作字段
    @Resource
    private CrifoService crifoService;
    @Resource
    private AbnormalService abnormalService;

    public void setCrifoService(CrifoService crifoService) {
        this.crifoService = crifoService;
    }

    @Override
    @Cacheable(cacheNames = "c1")
    public CrifoListVo getUnCheck() {
        LambdaQueryWrapper<Crifo> lambda = new QueryWrapper<Crifo>().lambda().eq(Crifo::getCrCheck, 0);
        List<Crifo> crifos = crifoService.list(lambda);
        CrifoListVo crifoListVo = new CrifoListVo();
        crifoListVo.setCrifoList(crifos);
        return crifoListVo;
    }

    @Override
    @Transactional
    public Map<String, Object> getPosition(Long crNo) {
        Crifo crifo = crifoService.getById(crNo);
        if (crifo == null) {
            throw new ApiException(ResultCode.ERROR);
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("crAddr", crifo.getCrAddr());
        return map;
    }

    @Override
    @Transactional
    public Map<String, Object> check(String crAddr, Long crNo) {
        Crifo crifo = crifoService.getById(crNo);
        if (crifo == null) {
            throw new ApiException(ResultCode.ERROR);
        }
        Map<String, Object> map = new HashMap<>(16);
        if (crAddr.equals(crifo.getCrAddr())) {
            map.put("isPosition", true);
        } else {
            map.put("isPosition", false);
        }
        return map;
    }

    @Override
    @Cacheable(cacheNames = "house")
    public HouseListVo getHouse() {
        HouseListVo houseListVo = new HouseListVo();
        QueryWrapper<Crifo> queryWrapper = new QueryWrapper<Crifo>().select("distinct crWarehouse").isNotNull("crWarehouse");
        houseListVo.setHouseList(crifoService.list(queryWrapper).stream().map((x) -> x.getCrWarehouse()).collect(Collectors.toList()));
        return houseListVo;
    }

    @Override
    @Cacheable(cacheNames = "c2")
    public CrifoListVo listByUnScan(Long[] crNoList) {
        CrifoListVo crifoListVo = new CrifoListVo();
        crifoListVo.setCrifoList(crifoService.listByIds(Arrays.asList(crNoList)).stream().filter(x -> x.getCrCheck() == false).collect(Collectors.toList()));
        return crifoListVo;
    }

    @Override
    public CrifoListVo listByScan(Long[] crNoList) {
        CrifoListVo crifoListVo = new CrifoListVo();
        crifoListVo.setCrifoList(crifoService.listByIds(Arrays.asList(crNoList)).stream().collect(Collectors.toList()));
        return crifoListVo;
    }

    @Override
    public String record(Long[] crNoList) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String strDate = formatter.format(date);
        for (Long aLong : crNoList) {
            Abnormal a = new Abnormal();
            a.setCrNo(aLong);
            a.setAbTime(date);
            a.setAbReason(aLong + "-" + strDate);
            abnormalService.save(a);
        }
        return "操作成功";
    }

    @Override
    @Transactional
    public String update(String crAddr, Long crNo) {

        Crifo crifo = crifoService.getById(crNo);
        if (crifo == null) {
            throw new ApiException(ResultCode.ERROR);
        }
        crifo.setCrAddr(crAddr);
        if (crifoService.update(crifo, null)) {
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    @Cacheable(cacheNames = "abnormal")
    public List<Abnormal> selectAbnormal() {
        List<Abnormal> abnormals=abnormalService.list();
        return abnormals;
    }


}
